package src.jdk8;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/5/26
 */
public class LambdaDemo {

    public static void main(String[] args) {

        LambdaDemo lambdaDemo = new LambdaDemo();

        System.out.println("开始体验Consumer~~~~");
        lambdaDemo.consumerTest();
        System.out.println("体验Consumer结束~~~~");

        System.out.println("开始体验Predicate~~~~");
        lambdaDemo.predicateTest();
        System.out.println("体验Predicate结束~~~~");

        System.out.println("开始体验Function~~~~");
        lambdaDemo.functionTest();
        System.out.println("体验Function结束~~~~");

    }

    /**
     * Function
     */
    private void functionTest() {
        Function<String, Integer> function = String::length;
        Integer len = function.apply("Hello");
        System.out.println(len);
    }


    /**
     * Consumer
     */
    private void consumerTest() {
        // 启动消费者
        Consumer<String> consumer = System.out::println;
        consumer.accept("Hello");
    }

    /**
     * Predicate
     */
    private void predicateTest() {
        // 断言函数
        Predicate<Integer> predicate = x -> x > 0;
        boolean is = predicate.test(0);
        // negate 我们定义的逻辑取反
        boolean test = predicate.negate().test(0);
        // and 等同于我们的逻辑与&&,存在短路特性
        Predicate<Integer> predicate1 = x -> x > 30;
        boolean test1 = predicate.and(predicate1).test(40);
        // or 等同于我们的逻辑或
        Predicate<Integer> predicate2 = x -> x == 0;
        boolean test2 = predicate.or(predicate2).test(0);
        System.out.println(is);
        System.out.println(test);
        System.out.println(test1);
        System.out.println(test2);
    }

}
